from utils.DBService import sqlquery
import os

devsql = """select ts.departname,wzdb.banhezhanminchen from t_s_depart as ts,WZDibangxinxi as wzdb where wzdb.departid = ts.ID and ts.ID in (\'{}\')"""

DEP_SQL = """WITH  dept
    AS (SELECT  *
        FROM   t_s_depart
        WHERE  parentdepartid = '{}'
        UNION ALL
        SELECT  d.*
        FROM   t_s_depart d
            INNER JOIN dept ON d.parentdepartid = dept.id
						 )
  SELECT id
  FROM  dept"""

def devStatic(Args,user):
    deps = sqlquery(DEP_SQL.format(user))
    print('SQL:\n\t', DEP_SQL.format(user))
    print(deps)
    depsid = []
    for dep in deps:
        depsid.append(dep[0])
    depId = '\',\''.join(depsid)
    cailiaoData = sqlquery(devsql.format(depId))
    print(devsql.format(depId))
    clData = [['站点', '设备']]
    for cailiao in cailiaoData:
        cailiao1 = ''
        if os.environ.get("ENV_NAME")=='1':
            cailiao1 = cailiao[1]
        else:
            cailiao1 = cailiao[1]
        clData.append([
            cailiao[0],
            cailiao1
        ])
    print(clData)
    return clData

functionConf = {
    "name": "devStatic",
    "description": "Query device and site information, display corresponding device information for the site during display, Be careful Weighbridge understood as geomagnetism",
    "parameters": {
        "type": "object",
        "properties": {
            # "site": {
            #     "type": "string",
            #     "description": "Department names, multiple departments separated by English commas, default to All，eg：A项目1标拌合站,A项目2标拌合站",
            # },
            # "device": {
            #     "type": "string",
            #     "description": "Device names, multiple devices separated by English commas, default to All, eg:1标地磅,2标地磅"
            # }
        },
        "required": []
    }
}